package com.qh.yyxt.controller;

import com.qh.yyxt.entity.Borrow;
import com.qh.yyxt.pojo.entity.meeting.DelayBorrow;
import com.qh.yyxt.pojo.BorrowDTO;
import com.qh.yyxt.pojo.BorrowPartDTO;
import com.qh.yyxt.service.BorrowService;
import com.qh.yyxt.service.DelayBorrowService;
import com.qh.yyxt.service.MeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class AdminBorrowController
{
    @Autowired
    private BorrowService borrowService;
    @Autowired
    private MeetingService meetingService;
    @Autowired
    private DelayBorrowService delayBorrowService;
    @RequestMapping("/admin/changeBorrowInfo")
    @ResponseBody
    public String changeBorrowInfo(int id,String name,String text,String starttime,String endtime)
    {
        return borrowService.changeBorrowById(id,name,text,starttime,endtime);
    }
    //管理员粗略查看borrow记录
    //不分页式的
    @RequestMapping("/admin/getPartAllBorrow")
    @ResponseBody
    public List<BorrowPartDTO> getPartAllBorrow()
    {
        List<BorrowPartDTO>dtoList=borrowService.findPartAllBorrow();
        return dtoList;
    }
    //管理员粗略查看延时审批的记录
    @RequestMapping("/admin/getDelay")
    @ResponseBody
    public  List<BorrowPartDTO>getDelay(){
        List<BorrowPartDTO>dtoList=delayBorrowService.getDelay();
        return dtoList;
    }
    //管理员查看某一个普通请求的详细信息
    @RequestMapping("/admin/getOneBorrow")
    @ResponseBody
    public BorrowDTO getOneBorrow(Integer id)
    {
        if(id!=null)
        return borrowService.findOneBorrow(id);
        else{
            return null;
        }
    }
    //关门的粗略查看borrow记录
    //不分页式的
    @RequestMapping("/admin2/getPartAllBorrow")
    @ResponseBody
    public List<BorrowPartDTO> admin2GetPartAllBorrow()
    {
        List<BorrowPartDTO>dtoList=borrowService.findPartAllBorrow();
        return dtoList;
    }
    //关门的查看某一个普通请求的详细信息
    @RequestMapping("/admin2/getOneBorrow")
    @ResponseBody
    public BorrowDTO admin2GetOneBorrow(Integer id)
    {
        if(id!=null)
            return borrowService.findOneBorrow(id);
        else{
            return null;
        }
    }
    @RequestMapping("/admin2/changeState")
    @ResponseBody
    public String admin2changeState(int bid,String state)
    {
        //System.out.println(bid+"  "+state);
        Borrow borrow=borrowService.findById(bid);
        //System.out.println(borrow);
        if(borrow!=null)
        {
            if(borrow.getState().equals("已撤销")||borrow.getState().equals("未通过")||borrow.getState().equals("会议结束"))
            {
                return "审批失败，注意查看请求的状态";
            }else
            {
                if (state.equals("会议结束"))
                {
                    DelayBorrow delayBorrow = delayBorrowService.getByBid(bid);
                    if (delayBorrow != null) {
                        // if(delayBorrow.getDelaystate())
                        if (borrowService.changeState(bid, state) == 1 && delayBorrowService.update(bid, state) == 1) {
                            return "true";
                        } else {
                            return "操作失败";
                        }
                    }
                    else
                    {
                        if (borrowService.changeState(bid, state) == 1) {
                            return "true";
                        } else {
                            return "操作失败";
                        }
                    }
                }
            }

        }
        return "未查询到相关请求，操作失败";
    }
    //管理员审批普通请求
    @RequestMapping("/admin/changeState")
    @ResponseBody
    public String changeState(int bid,String state)
    {
        Borrow borrow=borrowService.findById(bid);
        if(borrow!=null)
        {
            if(borrow.getState().equals("已撤销")||borrow.getState().equals("未通过")||borrow.getState().equals("会议结束"))
            {
                return "审批失败，注意查看请求的状态";
            }else
                {
                    if (state.equals("会议结束"))
                    {
                        DelayBorrow delayBorrow = delayBorrowService.getByBid(bid);
                        if (delayBorrow != null) {
                            // if(delayBorrow.getDelaystate())
                            if (borrowService.changeState(bid, state) == 1 && delayBorrowService.update(bid, state) == 1) {
                                return "true";
                            } else {
                                return "操作失败";
                            }
                        }
                    } else
                        {
                            if(state.equals("审批通过")) {
                                if (borrow.getState().equals("撤销待审批") == true)
                                {
                                    state = "已撤销";
                                }else if (borrowService.findBorrowByMidAndTime(borrow.getMid(),borrow.getStarttime(),borrow.getEndtime())!=null)
                                {
                                    return "操作失败！时间段冲突！";
                                }
                            }
                            if(state.equals("未通过"))
                            {
                                if (borrow.getState().equals("撤销待审批") == true) {
                                    state = "审批通过";
                                }
                            }

                            if (borrowService.changeState(bid, state) == 1)
                            {
                                return "true";
                            } else {
                                return "操作失败";
                            }

                         }
                }
        }
        return "未查询到相关请求，操作失败";
    }
    //管理员审批延时请求
    @RequestMapping("/admin/changeDelayState")
    @ResponseBody
    public String changeDelayState(int id,String state)
    {
            if(delayBorrowService.update(id,state)==1)
            {
                return "true";
            }else{
                return "操作失败";
            }
    }
}
